using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Exploder.Demo
{
	public class TargetManager : MonoBehaviour
	{
		private static TargetManager instance;

		public GameObject TargetObject;

		public TargetType TargetType;

		public Vector3 TargetPosition;

		public Image CrosshairGun;

		public Image CrosshairHand;

		public ExploderMouseLook MouseLookCamera;

		public Text PanelText;

		public static TargetManager Instance => instance;

		private void Awake()
		{
			instance = this;
		}

		private void Start()
		{
			ExploderUtils.SetActive(CrosshairGun.gameObject, status: true);
			ExploderUtils.SetActive(CrosshairHand.gameObject, status: true);
			ExploderUtils.SetActive(PanelText.gameObject, status: true);
		}

		private void Update()
		{
			Ray ray = MouseLookCamera.mainCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f));
			UnityEngine.Debug.DrawRay(ray.origin, ray.direction * 10f, Color.red, 0f);
			CrosshairGun.color = Color.white;
			TargetObject = null;
			TargetType = TargetType.None;
			TargetPosition = Vector3.zero;
			List<RaycastHit> list = new List<RaycastHit>(Physics.RaycastAll(ray, float.PositiveInfinity));
			GameObject gameObject = null;
			if (list.Count > 0)
			{
				list.Sort((RaycastHit a, RaycastHit b) => (MouseLookCamera.transform.position - a.point).sqrMagnitude.CompareTo((MouseLookCamera.transform.position - b.point).sqrMagnitude));
				gameObject = list[0].collider.gameObject;
				TargetPosition = list[0].point;
			}
			if (gameObject != null)
			{
				TargetObject = gameObject;
				if (IsDestroyableObject(TargetObject))
				{
					TargetType = TargetType.DestroyableObject;
				}
				else if (IsUseObject(TargetObject))
				{
					UseObject component = TargetObject.GetComponent<UseObject>();
					if ((bool)component && (MouseLookCamera.transform.position - component.transform.position).sqrMagnitude < component.UseRadius * component.UseRadius)
					{
						TargetType = TargetType.UseObject;
					}
				}
				else
				{
					TargetType = TargetType.Default;
				}
			}
			switch (TargetType)
			{
			case TargetType.DestroyableObject:
				CrosshairHand.enabled = false;
				CrosshairGun.enabled = true;
				CrosshairGun.color = Color.red;
				break;
			case TargetType.UseObject:
				CrosshairGun.enabled = false;
				CrosshairHand.enabled = true;
				PanelText.enabled = true;
				PanelText.text = TargetObject.GetComponent<UseObject>().HelperText;
				break;
			case TargetType.Default:
			case TargetType.None:
				CrosshairHand.enabled = false;
				CrosshairGun.enabled = true;
				CrosshairGun.color = Color.white;
				PanelText.enabled = false;
				break;
			}
			if (UnityEngine.Input.GetKeyDown(KeyCode.E) && TargetType == TargetType.UseObject)
			{
				UseObject component2 = TargetObject.GetComponent<UseObject>();
				if ((bool)component2)
				{
					component2.Use();
				}
			}
		}

		private bool IsDestroyableObject(GameObject obj)
		{
			if (obj.CompareTag("Exploder"))
			{
				return true;
			}
			if ((bool)obj.transform.parent)
			{
				return IsDestroyableObject(obj.transform.parent.gameObject);
			}
			return false;
		}

		private bool IsUseObject(GameObject obj)
		{
			if (obj.CompareTag("UseObject"))
			{
				return true;
			}
			if ((bool)obj.transform.parent)
			{
				return IsDestroyableObject(obj.transform.parent.gameObject);
			}
			return false;
		}
	}
}
